import 'dart:async';
import 'package:flame/components.dart' hide Timer;
import 'package:flame/events.dart';
import 'package:flame/flame.dart';
import 'package:flame/layout.dart';
import 'package:flutter/material.dart';
import 'package:freedom/util/date_util.dart';
import '../common/mahjong_context.dart';
import '../mahjong_game.dart';

class MahjongTips extends PositionComponent
    with TapCallbacks, HasGameRef<MahjongIndexGame> {
  var microphoneImage = Flame.images.fromCache('mahjong/util/microphone.png');

  MahjongTips();

  @override
  bool containsPoint(Vector2 point) {
    return Rect.fromLTWH(0, 0, size.x, size.y)
        .contains(Offset(point.x, point.y));
  }

  @override
  void onTapDown(TapDownEvent event) async {}

  @override
  FutureOr<void> onLoad() {
    size = Vector2(MahjongContext().headImageHeight * 2,
        MahjongContext().headImageHeight / 2);
    position = Vector2(
        MahjongContext().menuPosition.x - size.x * 1.5,
        MahjongContext().topHandAreaPoint.y +
            (MahjongContext().topHandCardSize.y - size.y) / 2);
    anchor = Anchor.topLeft;
    // debugMode = true;
    var textCom = TextComponent(
        text: "",
        textRenderer: TextPaint(
            style: TextStyle(
                fontSize: size.y * 0.8,
                color: Colors.white,
                fontFamily: 'Menlo',
                shadows: const [
              Shadow(color: Colors.red, offset: Offset(1, 1), blurRadius: 1),
            ])));
    textCom.text = DateUtil.getTime(format: "HH:mm");
    add(TimerComponent(
        period: 60,
        onTick: () {
          textCom.text = DateUtil.getTime(format: "HH:mm");
        }));

    add(AlignComponent(
      alignment: const Anchor(0.1, 0.5),
      child: textCom,
    ));

    add(AlignComponent(
        alignment: const Anchor(0.9, 0.5),
        child: SpriteComponent(
            sprite:
                Sprite(Flame.images.fromCache('mahjong/util/game_info.png')),
            size: Vector2.all(size.y * 0.8))));

    return super.onLoad();
  }

  @override
  void render(Canvas canvas) {
    final Paint paint = Paint()
      ..color = Color.fromARGB(110, 136, 136, 136)
      ..style = PaintingStyle.fill
      ..strokeWidth = 1.0;
    RRect rect =
        RRect.fromLTRBR(size.x, size.y, 0, 0, const Radius.circular(0));
    canvas.drawRRect(rect, paint);

    super.render(canvas);
  }
}
